import erdfServer from "erdf-server";
import merge from "@/utils/merge";
import { openLoading, closeLoading } from "@/utils/loading";

export default {
  install(_Vue) {
    erdfServer().config({
      //addOrUpdate方法默认值(0为update,1为updateFull,2为updateIncrement)
      updateType: 0,
      errorHandler: function (error) {
        /*全局异常处理
         *返回true--不在进入后面的catch异常捕获
         *返回false--进入后面的catch异常捕获
         */

        if (error.data && error.data.code && error.data.code == 401) {
          //代表登录失效
          // window.location.href = "/login";  
          sessionStorage.removeItem("authorization_token"); //移除token
          window.location.reload;
          return true; //不在进入后面的catch异常捕获
        }
        return false;
      },
      secretKey: "evget",
    });

    let globalConfig = {
      beforeRequest: function (value) {
        //判断接口是否需要加载动画
        const noLoadingUrlList = [
          '/server/function-user/base/ResourceUser/selects',
          '/server/function-user/base/ResourceUser/insert'
        ]
        if (noLoadingUrlList.indexOf(value.url) === -1) {
          openLoading();
        }
        
      },
      afterResponse: function () {
        closeLoading();
      },
    };
    _Vue.prototype.$server = (config) => {
      if (!config) config = {};
      if (sessionStorage.getItem("authorization_token")) {
        config.token = sessionStorage.getItem("authorization_token");
      }
      return erdfServer(merge({}, globalConfig, config));
    };
  },
};
